<?php

/**
 * admin actions.
 *
 * @package    travis
 * @subpackage admin
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class adminActions extends sfActions {

    public function executeAdmin(sfWebRequest $request) {
        $this->setLayout('adminLayout');
        $this->createData();

        $this->sub = 'Main';
    }

    public function executeAdminUsers(sfWebRequest $request) {
        $this->setLayout('adminLayout');
        $this->createData();

        $permissions = $request->getPostParameter('permissions');

        if (isset($permissions)) {
            $this->users = tblUsersTable::getInstance()->findByDql('permissions=?', $permissions);
        } else {
            $this->users = Doctrine_Core::getTable('tblUsers')->createQuery('a')->execute();
        }

        $this->sub = 'Users';
    }

    public function executeAdminEditUser(sfWebRequest $request) {
        $this->setLayout('adminLayout');
        $this->createData();
      
        $id = $request->getParameter('user_id');

        $this->firm = tblUsersTable::getInstance()->findByDql('permissions=? and status=?', array(sbValues::PERMISSIONS_FIRM, sbValues::STATUS_ACTIVE));
        $this->country = Doctrine_Core::getTable('tblCountry')->createQuery('a')->execute();
        $this->city = Doctrine_Core::getTable('tblCity')->createQuery('a')->execute();
        $this->user = tblUsersTable::getInstance()->findByDql('id=?', $id)->getFirst();
        $this->structure = tblUsersStructureTable::getInstance()->findByDql('subsidiary_id=?', $id)->getFirst();

        $this->sub = 'Users';
    }

    public function executeAdminAddUser(sfWebRequest $request) {
        $this->setLayout('adminLayout');
        $this->createData();

        $this->firm = tblUsersTable::getInstance()->findByDql('permissions=? and status=?', array(sbValues::PERMISSIONS_FIRM, sbValues::STATUS_ACTIVE));
        $this->country = Doctrine_Core::getTable('tblCountry')->createQuery('a')->execute();
        $this->city = Doctrine_Core::getTable('tblCity')->createQuery('a')->execute();

        $this->sub = 'Users';
    }

    public function executeAdminDeactivateUser(sfWebRequest $request) {
        $id = $request->getParameter('user_id');
        sbData::deactivateUser($id);
        $this->redirect('ausers');
    }

    public function executeAdminCountry(sfWebRequest $request) {
        $this->setLayout('adminLayout');
        $this->createData();

        $this->country = Doctrine_Core::getTable('tblCountry')->createQuery('a')->execute();

        $this->sub = 'Country';
    }

    public function executeAdminAddCountry(sfWebRequest $request) {
        $this->setLayout('adminLayout');
        $this->createData();

        $this->sub = 'Country';
    }

    public function executeAdminEditCountry(sfWebRequest $request) {
        $this->setLayout('adminLayout');
        $this->createData();

        $id = $request->getParameter('group_id');

        $this->sub = 'Country';

        $this->country = tblCountryTable::getInstance()->findByDql('id=?', $id)->getFirst();
    }

    public function executeAdminRegion(sfWebRequest $request) {
        $this->setLayout('adminLayout');
        $this->createData();

        $this->region = Doctrine_Core::getTable('tblRegion')->createQuery('a')->execute();

        $this->sub = 'Region';
    }

    public function executeAdminAddRegion(sfWebRequest $request) {
        $this->setLayout('adminLayout');
        $this->createData();

        $this->country = Doctrine_Core::getTable('tblCountry')->createQuery('a')->execute();

        $this->sub = 'Region';
    }

    public function executeAdminEditRegion(sfWebRequest $request) {
        $this->setLayout('adminLayout');
        $this->createData();

        $id = $request->getParameter('region_id');

        $this->sub = 'Region';

        $this->country = Doctrine_Core::getTable('tblCountry')->createQuery('a')->execute();
        $this->region = tblRegionTable::getInstance()->findByDql('id=?', $id)->getFirst();
    }

    public function executeAdminCity(sfWebRequest $request) {
        $this->setLayout('adminLayout');
        $this->createData();

        $this->city = Doctrine_Core::getTable('tblCity')->createQuery('a')->execute();

        $this->sub = 'City';
    }

    public function executeAdminAddCity(sfWebRequest $request) {
        $this->setLayout('adminLayout');
        $this->createData();

        $this->country = Doctrine_Core::getTable('tblCountry')->createQuery('a')->execute();
        $this->region = Doctrine_Core::getTable('tblRegion')->createQuery('a')->execute();

        $this->sub = 'City';
    }

    public function executeAdminEditCity(sfWebRequest $request) {
        $this->setLayout('adminLayout');
        $this->createData();

        $id = $request->getParameter('city_id');

        $this->country = Doctrine_Core::getTable('tblCountry')->createQuery('a')->execute();
        $this->region = Doctrine_Core::getTable('tblRegion')->createQuery('a')->execute();
        $this->city = tblCityTable::getInstance()->findByDql('id=?', $id)->getFirst();

        $this->sub = 'City';
    }

    public function executeAdminProvider(sfWebRequest $request) {
        $this->setLayout('adminLayout');
        $this->createData();

        $this->providers = Doctrine_Core::getTable('tblProvider')->createQuery('a')->execute();

        $this->sub = 'Provider';
    }

    public function executeAdminAddProvider(sfWebRequest $request) {
        $this->setLayout('adminLayout');
        $this->createData();

        $this->sub = 'Provider';
    }

    public function executeAdminEditProvider(sfWebRequest $request) {
        $this->setLayout('adminLayout');
        $this->createData();

        $id = $request->getParameter('provider_id');

        $this->provider = tblProviderTable::getInstance()->findByDql('id=?', $id)->getFirst();

        $this->sub = 'Provider';
    }

    public function executeAdminHotel(sfWebRequest $request) {
        $this->setLayout('adminLayout');
        $this->createData();

        $page = $request->getParameter('page');
//        $get = sbAdmin::getAdminHotel($page);
//        $this->hotels = $get['hotels'];
//        $this->count = $get['count'];
        $this->names = Doctrine_Core::getTable('tblHotel')->createQuery('a')->execute();
        $this->city = Doctrine_Core::getTable('tblCity')->createQuery('a')->execute();
        $this->provider = Doctrine_Core::getTable('tblProvider')->createQuery('a')->execute();

        $this->sub = 'Hotels';
    }

    public function executeAdminAddHotel(sfWebRequest $request) {
        $this->setLayout('adminLayout');
        $this->createData();

        $this->country = Doctrine_Core::getTable('tblCountry')->createQuery('a')->execute();
        $this->region = Doctrine_Core::getTable('tblRegion')->createQuery('a')->execute();
        $this->city = Doctrine_Core::getTable('tblCity')->createQuery('a')->execute();
        $this->provider = Doctrine_Core::getTable('tblProvider')->createQuery('a')->execute();

        $this->sub = 'Hotels';
    }

    public function executeAdminHotelDetails(sfWebRequest $request) {
        $this->setLayout('adminLayout');
        $this->createData();
        $this->sub = 'Hotels';

        $hotel_id = $request->getParameter('id');
        $hotel = new sbHotels();
        $this->hotel = $hotel->moreHotel($hotel_id);
    }

    public function executeAdminEditHotel(sfWebRequest $request) {
        $this->setLayout('adminLayout');
        $this->createData();

        $id = $request->getParameter('hotel_id');

        $this->hotel = tblHotelTable::getInstance()->findByDql('id=?', $id)->getFirst();
        $this->country = Doctrine_Core::getTable('tblCountry')->createQuery('a')->execute();
        $this->region = Doctrine_Core::getTable('tblRegion')->createQuery('a')->execute();
        $this->city = Doctrine_Core::getTable('tblCity')->createQuery('a')->execute();
        $this->provider = Doctrine_Core::getTable('tblProvider')->createQuery('a')->execute();

        $this->sub = 'Hotels';
    }

    public function executeAdminDeleteHotel(sfWebRequest $request) {
        $id = $request->getParameter('hotel_id');
        sbData::deleteHotel($id);
        $this->redirect('ahotels');
    }

    public function executeAdminPlans(sfWebRequest $request) {
        $this->setLayout('adminLayout');
        $this->createData();

        $this->plans = Doctrine_Core::getTable('tblPlans')->createQuery('a')->execute();

        $this->sub = 'Plans';
    }

    public function executeAdminAddPlan(sfWebRequest $request) {
        $this->setLayout('adminLayout');
        $this->createData();

        $this->sub = 'Plans';
    }

    public function executeAdminEditPlan(sfWebRequest $request) {
        $this->setLayout('adminLayout');
        $this->createData();

        $id = $request->getParameter('plan_id');

        $this->plan = tblPlansTable::getInstance()->findByDql('id=?', $id)->getFirst();

        $this->sub = 'Plans';
    }

    public function executeAdminRooms(sfWebRequest $request) {
        $this->setLayout('adminLayout');
        $this->createData();
        $hotel_id = $request->getParameter('hotel_id');

        $this->hotel = tblHotelTable::getInstance()->findByDql('id=?', $hotel_id)->getFirst();

        $this->rooms = Doctrine_Query::create()->
                select('*')->
                from('tblRooms')->
                where('hotel_id=? and status=?', array($hotel_id, sbValues::STATUS_ACTIVE))->
                execute();

        $this->sub = 'Hotels';
    }

    public function executeAdminAddRoom(sfWebRequest $request) {
        $this->setLayout('adminLayout');
        $this->createData();
        $hotel_id = $request->getParameter('hotel_id');

        $this->hotels = tblHotelTable::getInstance()->findByDql('id=?', $hotel_id)->getFirst();

        $this->sub = 'Hotels';
    }

    public function executeAdminEditRoom(sfWebRequest $request) {
        $this->setLayout('adminLayout');
        $this->createData();

        $id = $request->getParameter('room_id');

        $this->room = Doctrine_Query::create()->
                select('r.*, h.name_en')->
                from('tblRooms r')->
                leftJoin('r.HotelRooms h')->
                where('r.id=?', $id)->
                execute();

        $this->sub = 'Hotels';
    }

    public function executeAdminDeleteRoom(sfWebRequest $request) {
        $room_id = $request->getParameter('room_id');
        $hotel_id = $request->getParameter('hotel_id');

        $room = tblRoomsTable::getInstance()->findByDql('id=? and hotel_id=?', array($room_id, $hotel_id))->getFirst();
        if ($room) {
            $room->setStatus(sbValues::STATUS_DEACTIVE);
            $room->setUpdateTime(sbValues::getCurrentTime());
            $room->save();
        }

        $this->redirect('arooms', array('hotel_id' => $hotel_id));
    }

    public function executeAdminSearchPrice(sfWebRequest $request) {
        $this->setLayout('adminLayout');
        $this->createData();
        $this->sub = 'Prices';

        $city_id = $request->getParameter('city');
        $hotel_id = $request->getParameter('hotel');
        $room_id = $request->getParameter('room');
        $from_date = $request->getParameter('from');
        $to_date = $request->getParameter('to');

        if (is_null($city_id) && is_null($hotel_id) && is_null($room_id) && is_null($from_date) && is_null($to_date)) {
            $this->prices = array();
            $this->arrSearch = array(
                'city' => 0,
                'hotel' => 0,
                'room' => 0,
                'from' => '',
                'to' => '',
                'string' => ''
            );
        } else {
            $prc = new sbPrices();
            $this->prices = $prc->getPrices($hotel_id, $room_id, $from_date, $to_date);
            $this->arrSearch = array(
                'city' => $city_id,
                'hotel' => $hotel_id,
                'room' => $room_id,
                'from' => $from_date,
                'to' => $to_date,
                'string' => $prc->createStringDescription($hotel_id, $room_id, $from_date, $to_date)
            );
        }

        $this->city = sbValues::allCity();
        $this->hotels = sbValues::allHotels();
        $this->rooms = sbValues::allRooms();
    }

    public function executeUpdateCurrentPrice(sfWebRequest $request) {
        $dPrice = array(
            'id' => $request->getPostParameter('id'),
            'fit_usd' => $request->getPostParameter('fit_usd'),
            'fit_nis' => $request->getPostParameter('fit_nis'),
            'cascade_usd' => $request->getPostParameter('cascade_usd'),
            'cascade_nis' => $request->getPostParameter('cascade_nis'),
            'special_usd' => $request->getPostParameter('special_usd'),
            'special_nis' => $request->getPostParameter('special_nis'),
            'market_usd' => $request->getPostParameter('market_usd')
        );

        $days = $request->getPostParameter('days');
        $period = $request->getPostParameter('period');
        $roomId = $request->getPostParameter('room');
        $hotelId = $request->getPostParameter('hotel');
        $from = $request->getPostParameter('from');
        $to = $request->getPostParameter('to');

        $prc = new sbPrices();
        if (is_null($days) && is_null($period) && is_null($roomId)) {
            $result = $prc->updateCurrentPrice($dPrice);
        } else {
            $result = $prc->updatePeriodPrices($dPrice, $days, $period, $hotelId, $roomId, $from, $to);
        }

        die(json_encode($result));
    }

    public function createData() {
        $cookie = $this->getRequest()->getCookie('troli');

        if ($cookie) {
            if (self::checkTime($cookie)) {
                self::updateData($cookie);
                $data = base64_decode($cookie);
                $data = explode("/", $data);
                $this->name = sbUser::getFullUserName($data[2]);
            } else {
                $this->redirect('homepage');
            }
        } else {
            $this->redirect('homepage');
        }
    }

    public function checkTime($data) {
        if ($data) {
            $data = explode("/", base64_decode($data));
            if ($data[3] + 10 * 60 > time() || $data[4] == 'online') {
                return true;
            }

            return false;
        }

        return false;
    }

    //update time if session is life
    public function updateData($data) {
        if ($data) {
            $data = explode("/", base64_decode($data));
            $data[3] = time();

            $ndata = $data[0] . '/' . $data[1] . '/' . $data[2] . '/' . $data[3];

            return base64_encode($ndata);
        }

        return false;
    }

}
